package com.yuntu.controller;

import com.yuntu.pojo.Dev;
import com.yuntu.service.DevService;
import com.yuntu.tools.PageUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/dev")
public class DevController {
    private Logger logger=Logger.getLogger(DevController.class);
    @Autowired
    private DevService devService;
    //登录
    @RequestMapping(value = "/dologin",method = RequestMethod.POST)
    public String doLogin(String devCode, String devPassword, HttpServletRequest request, HttpSession session) throws Exception {
        logger.debug("==========================doLogin");
        Dev user=devService.login(devCode,devPassword);
        if (null!=user){
            session.setAttribute("userSession",user);
            return "index";
        }else{
            request.setAttribute("error","用户密码不正确");
            return "login";
        }
    }
    //查询全部
    @RequestMapping(value = "/list")
    public String PeitemsList(HttpServletRequest request){
        try {
            List<Dev> peitemsList= devService.devAll();
            request.setAttribute("listServie",peitemsList);
            System.out.println(peitemsList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "index";
    }
    //添加页面显示
    @RequestMapping(value = "/add")
    public String add(){
        return "addDev";
    }

    //添加信息
    @RequestMapping(value = "/add.do")
    public String addDev(Dev dev)throws Exception{
        if (devService.add(dev)){
            return "redirect:/dev/list";
        }
        return "addDev";
    }
    //根据id删除
    @RequestMapping(value = "/del")
    public String delPeitems(@RequestParam int id) throws Exception{
        if (devService.del(id)){
            return "redirect:/dev/list";
        }
        return "index";
    }
    //根据id查询信息
    @RequestMapping("/updateById")
    public String getById(@RequestParam int id, HttpServletRequest request)throws Exception{
        Dev param=devService.getId(id);
        Object[] pr={param.getId(),
                param.getDevCode(),
                param.getDevName(),
                param.getDevPassword(),
                param.getDevEmail(),
                param.getDevInfo(),
                param.getModifyBy(),
                param.getModifyDate(),
                param.getCreatedBy(),
                param.getCreationDate()
        };
        request.setAttribute("pr",pr);
        return "updDev";
    }
    //修改
    @RequestMapping(value = "/update.do")
    public String updateDev(Dev dev) throws Exception{
        if(devService.upd(dev)){
            return "redirect:/dev/list";
        }
        return "updDev";
    }
    //分页
    @RequestMapping(value = "/pageList",method = RequestMethod.GET)
    public String pageLists(Integer pageIndex,HttpSession session)throws Exception{
        if (pageIndex==null){
            pageIndex=1;
        }
        PageUtil<Dev> pageUtil=new PageUtil<>();
        pageUtil.setPageindex(pageIndex);
        pageUtil.setPagesize(2);
        devService.getPageUtil(pageUtil);
        session.setAttribute("pageUtil",pageUtil);
        return "index";
    }
}
